package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.domain.clue.CluePoolDO;
import com.qk.dto.clue.*;
import com.qk.entity.Clue;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 线索控制层
 * @Author:Mr.m
 * @Date: 2025-10-13 20:23
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;

    //线索池查询
    @GetMapping("/pool")
    public Result cluePool(
            @RequestParam(value = "clueId", required = false) Integer clueId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "channel", required = false) Integer channel,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        // 构造CluePollListDTO对象
        CluePoolListDTO dto = new CluePoolListDTO();
        dto.setClueId(clueId);
        dto.setPhone(phone);
        dto.setChannel(channel);
        dto.setPage(page);
        dto.setPageSize(pageSize);

        // 调用Service方法
        PageResult<CluePoolDO> result = clueService.cluePoolList(dto);
        return Result.success(result);
    }

    //转伪线索
    @PutMapping("/false/{id}")
    public Result toFalse(@PathVariable Integer id ,@RequestBody ClueFalseDTO dto) {
        clueService.toFalse(id, dto);
        return Result.success();
    }

    //转商机
    @PutMapping("toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        clueService.toBusiness(id);
        return Result.success();
    }


    //修改线索
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto) {
        // TODO 验证每一个请求都会开辟一条线程池中线程处理
        log.info("=========================Controller 中 方法updateClueInfoById 处理线程名称是：{}",Thread.currentThread().getName());
        clueService.updateClueInfoById(dto);
        return Result.success();
    }

    //根据id查询线索
    @GetMapping("/{id}")
    public Result getClueInfoById(@PathVariable Integer id){
        ClueVO vo = clueService.getClueInfoById(id);
        return Result.success(vo);
    }

    //线索分配
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateClueById(@PathVariable Integer clueId, @PathVariable Integer userId) {
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    //分页查询线索列表
    @GetMapping
    public Result list(ClueListDTO dto){
        PageResult<ClueDO> result = clueService.clueList(dto);
        return Result.success(result) ;
    }

    //新增线索
    @PostMapping
    public Result addCule(@RequestBody ClueDTO dto){
        clueService.addClue(dto);
        return Result.success();
    }

}
